import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../views/Main.vue";
import login from "../views/login.vue";
import register from "../views/register.vue";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/login",
      name: "login",
      component: login,
      meta: { isPublic: true },
    },
    {
      path: "/register",
      name: "register",
      component: register,
      meta: { isPublic: true },
    },
    {
      path: "/",
      name: "main",
      component: Main,
      children: [
        //category分类
        {
          path: "/categories/create",
          component: () => import("../views/category/CategoryEdit.vue"),
        },
        {
          path: "/categories/edit/:id",
          component: () => import("../views/category/CategoryEdit.vue"),
          props: true,
        },
        //任何元素都注入到categoryEdit页面里面  在页面内使用id。在页面内用props接收
        {
          path: "/categories/list",
          component: () => import("../views/category/CategoryList.vue"),
        },

        //car车辆
        {
          path: "/cars/create",
          component: () => import("../views/car/CarEdit.vue"),
        },
        {
          path: "/cars/edit/:id",
          component: () => import("../views/car/CarEdit.vue"),
          props: true,
        },
        {
          path: "/cars/list",
          component: () => import("../views/car/CarList.vue"),
        },

        //collect收藏
        {
          path: "/collects/create",
          component: () => import("../views/collect/CollectEdit.vue"),
        },
        {
          path: "/collects/edit/:id",
          component: () => import("../views/collect/CollectEdit.vue"),
          props: true,
        },
        {
          path: "/collects/list",
          component: () => import("../views/collect/CollectList.vue"),
        },

        //shop商店
        {
          path: "/shops/create",
          component: () => import("../views/shop/ShopEdit.vue"),
        },
        {
          path: "/shops/edit/:id",
          component: () => import("../views/shop/ShopEdit.vue"),
          props: true,
        },
        {
          path: "/shops/list",
          component: () => import("../views/shop/ShopList.vue"),
        },
        //user用户
        {
          path: "/users/create",
          component: () => import("../views/user/UserEdit.vue"),
        },
        {
          path: "/users/edit/:id",
          component: () => import("../views/user/UserEdit.vue"),
          props: true,
        },
        {
          path: "/users/list",
          component: () => import("../views/user/UserList.vue"),
        },

        //article文章
        {
          path: "/articles/create",
          component: () => import("../views/article/ArticleEdit.vue"),
        },
        {
          path: "/articles/edit/:id",
          component: () => import("../views/article/ArticleEdit.vue"),
          props: true,
        },
        {
          path: "/articles/list",
          component: () => import("../views/article/ArticleList.vue"),
        },
        //ad广告
        {
          path: "/ads/create",
          component: () => import("../views/ad/AdEdit.vue"),
        },
        {
          path: "/ads/edit/:id",
          component: () => import("../views/ad/AdEdit.vue"),
          props: true,
        },
        {
          path: "/ads/list",
          component: () => import("../views/ad/AdList.vue"),
        },

        //adminuser用户管理员
        {
          path: "/admin_users/create",
          component: () => import("../views/adminuser/AdminUserEdit.vue"),
        },
        {
          path: "/admin_users/edit/:id",
          component: () => import("../views/adminuser/AdminUserEdit.vue"),
          props: true,
        },
        {
          path: "/admin_users/list",
          component: () => import("../views/adminuser/AdminUserList.vue"),
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next("/login");
  }
  console.log(to.meta);
  next();
});
export default router;
